<%@page import="java.util.TreeMap"%>
<%@page import="java.util.Map"%>
<%@page import="entities.MetaSeo"%>
<%@page import="systems.ConfigSystem"%>
<%@page import="entities.SiteConfig"%>
<%@page import="helpers.LanguageHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="java.util.List"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.User"%>
<%@page import="entities.Category"%>
<%@page import="entities.Article"%>
<%
SiteConfig siteConfig = ConfigSystem.getConfigs();
Article article = (Article) request.getAttribute("article");
List<Article> cArticles = (List<Article>) request.getAttribute("cArticles");
List<Article> uArticles = (List<Article>) request.getAttribute("uArticles");
Category category = (Category) request.getAttribute("category");
User author = (User) request.getAttribute("author");
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
LanguageHelper translate = (LanguageHelper) request.getAttribute("translate");
MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__ARTICLE_TITLE__", article.getTitle());
replates.put("__ARTICLE_CONTENT__", StringHelper.cutHtmlToText(article.getContent(), 150));
replates.put("__ARTICLE_ALIAS__", article.getAlias());


String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/ezsarticle/layouts/header.jsp" >
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>

		<div class="row-fluid">
			<div class="span8">
				<article>
					<h2><%=article.getTitle() %></h2>
					<p class="meta">
						<i class="icon-calendar"></i> <%=DateHelper.getStringFromDate(article.getCreated(), "MMM dd, yyyy") %> - 
						<% if (category != null) { %>
						<i class="icon-folder-open"></i> <a href="/category/<%=category.getAlias() %>"><%=category.getName() %></a> - 
						<% } %>
						<% if (author != null) { %>
						<i class="icon-user"></i> <a href="/author/<%=author.getId()%>"><%=author.getName() %></a>
						<% } else if (!article.getAuthorName().isEmpty()) { %>
						<i class="icon-user"></i> <%=article.getAuthorName() %>
						<% } %>
					</p>
					
					<div class="article-content">
						<%=article.getContent() %>
					</div>
					
					<% if (article.getKeywords().size() > 0) { %>
					<p class="tags">
						Tags : 
						<% for (int i = 0; i < article.getKeywords().size(); i++) { %>
                    	<%= i > 0 ? ", " : "" %>
						<a rel="tag" href="/tag/<%=URLEncoder.encode(article.getKeywords().get(i)) %>"><%=article.getKeywords().get(i) %></a>
						<% } %>
					</p>
					<% } %>
					
				</article>
				
				<%  if (uArticles != null && uArticles.size() > 1) { %>
				<h3>Author's articles</h3>
				<div class="block">
					<div class="block-content">
						<ul>
							<%
                    		for (int i = 0; i < uArticles.size(); i++) {
                    			if (uArticles.get(i).getId().equals(article.getId())) break;
                    		%>
							<li><a href="/article/<%=uArticles.get(i).getAlias()%>"><%=uArticles.get(i).getTitle()%></a></li>
							<% } %>
						</ul>
					</div>
				</div>
				<% } %>
				
				<% if (cArticles != null && cArticles.size() > 1) { %>
				<h3>Other articles</h3>
				<div class="block">
					<div class="block-content">
						<ul>
							<% 
                    		for (int i = 0; i < cArticles.size(); i++) {
                    			if (cArticles.get(i).getId().equals(article.getId())) break;
                    		%>
							<li><a href="/article/<%=cArticles.get(i).getAlias()%>"><%=cArticles.get(i).getTitle()%></a></li>
							<% } %>
						</ul>
					</div>
				</div>
				<% } %>
				
			</div>
			
			<jsp:include page="/templates/ezsarticle/elements/right.jsp" />
			
		</div>

<jsp:include page="/templates/ezsarticle/layouts/footer.jsp" />